        var websocket = null;
        var jsonobj=null;
        //判断当前浏览器是否支持WebSocket
        var onlineUser;
        var GROUP_DATA;
        var nm_="";
        var np_="";
        function connection(nm,np){
        	nm_=nm;
        	np_=np;
	        	 //ws://echo.websocket.org
	        if('WebSocket' in window){
	//            websocket = new WebSocket("ws://"+window.top.location.host+"/fx/login_websocket");
//	            websocket = new WebSocket("ws://"+sy()+"/chat/foo/bar");
	            websocket = new WebSocket("ws://"+sy()+"/chat/"+nm+"/"+np);
	            //连接发生错误的回调方法
		        websocket.onerror = function(ev,data){
		        	 alert('连接发生错误的回调方法');
		            //setMessageInnerHTML("error");
		        	// disConnect();
		        };
		
		        //连接成功建立的回调方法
		      websocket.onopen = function(){
		    	  alert('连接成功建立的回调方法');
		          //setMessageInnerHTML("websocket握手成功！");
		      };
		
		      //接收到消息的回调方法
		      websocket.onmessage = function(event){
		    	  jsonobj=eval('('+event.data+')');
		    		//alert(jsonobj.msg.type);
				   switch (jsonobj.msg.type) {
					case 'LOGIN':
						   loginHandler(jsonobj);
						break;
			
					case 'ONLINE':
						   onlineHandler(jsonobj);
						break;
					case 'GET_USER_GROUP':
						GROUP_DATA=jsonobj ;
						break;
					}
		      };
				      //连接关闭的回调方法
		      websocket.onclose = function(event){
		    	  websocket.close();
		    	  alert('连接关闭的回调方法');
		         // setMessageInnerHTML("close");
		    	  disConnect();
		      };

	        }else{
	            alert('不支持websocket');
	        }
        }
       //检查链接，短线重连
        var disConnect = function(){
            setTimeout(function(){
            	connection(nm_,np_);
            },5000);
        }

      //登陆成功提醒
      function loginHandler(jsonobj){
    	  if(jsonobj.msg.code=='SUCCESS'){
    		  tipsHandler(jsonobj.msg.msg);
    		  onlineUser=jsonobj.user;
    		  
    	  }
      }
     //上下线更新列表
      function onlineHandler(jsonobj){
    	  tipsHandler(jsonobj.msg.msg);
    	  
    	  alert(jsonobj.onlinelist)
      }
    //分组
      function tipsHandler(msg){
    	  alert(msg)
      }
     
      
      //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
      window.onbeforeunload = function(){
    	  alert('监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。');
          websocket.close();
      };

      //将消息显示在网页上
      function setMessageInnerHTML(innerHTML){
        //  document.getElementById('message').innerHTML += innerHTML + '<br/>';
      }

      //关闭连接
      function closeWebSocket(){
    	  alert("关闭closeWebSocket");
          websocket.close();
      }
      //发送消息
        function send(){
            websocket.send("来自客户端的消息");
        }
        //获取用户排队信息
        function getGroup(num){
        	 var job="{'from':'"+onlineUser.name+"','msg':'GET_USER_GROUP' ,'to':'"+num+"','time':'"+new Date()+"','type':'GETDATA'}";
             websocket.send(job.toString());
        }
        
      	function sy(){
      		var curWwwPath = window.document.location.href;
      		var pathName =  window.document.location.pathname;
      		var pos = curWwwPath.indexOf(pathName);
      		var localhostPaht = curWwwPath.substring(0,pos);
      		var projectName = pathName.substring(0,pathName.substr(1).indexOf('/')+1);
      		var ip=window.location.host;
      		var prot=window.location.port;
      		return (ip + projectName);
      		}
